<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之多范式编程语言 Scala02_变量和数据类型 | Oct25-X</title><meta name="keywords" content="Scala"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Scala 的变量一、注释Scala 注释使用和 Java 完全一样。 12345678910111213141516171819202122package org.example.chapter02&#x2F;** * @Description TODO Scala注释 *&#x2F;object Scala01_Comment &amp;#123;  &#x2F;**   * 文档注释   * 这是程序入口方法   *    *">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之多范式编程语言 Scala02_变量和数据类型">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="Scala 的变量一、注释Scala 注释使用和 Java 完全一样。 12345678910111213141516171819202122package org.example.chapter02&#x2F;** * @Description TODO Scala注释 *&#x2F;object Scala01_Comment &amp;#123;  &#x2F;**   * 文档注释   * 这是程序入口方法   *    *">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt45.png">
<meta property="article:published_time" content="2024-02-18T12:30:11.000Z">
<meta property="article:modified_time" content="2024-02-24T11:05:18.774Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Scala">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt45.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之多范式编程语言 Scala02_变量和数据类型',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-24 19:05:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg19.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之多范式编程语言 Scala02_变量和数据类型</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-02-18T12:30:11.000Z" title="发表于 2024-02-18 20:30:11">2024-02-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-24T11:05:18.774Z" title="更新于 2024-02-24 19:05:18">2024-02-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之多范式编程语言 Scala02_变量和数据类型"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Scala-的变量"><a href="#Scala-的变量" class="headerlink" title="Scala 的变量"></a>Scala 的变量</h1><h2 id="一、注释"><a href="#一、注释" class="headerlink" title="一、注释"></a>一、注释</h2><p>Scala 注释使用和 Java 完全一样。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala注释</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala01_Comment</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 文档注释</span></span><br><span class="line"><span class="comment">   * 这是程序入口方法</span></span><br><span class="line"><span class="comment">   * </span></span><br><span class="line"><span class="comment">   * @param args main方法参数</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">//单行注释</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    这是一个简单的多行注释</span></span><br><span class="line"><span class="comment">    可以写多行的注释内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、变量和常量"><a href="#二、变量和常量" class="headerlink" title="二、变量和常量"></a>二、变量和常量</h2><h3 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> 变量名 [: 变量类型] = 初始值		示例：<span class="keyword">var</span> i:<span class="type">Int</span> = <span class="number">10</span></span><br><span class="line"><span class="keyword">val</span> 常量名 [: 常量类型] = 初始值		示例：<span class="keyword">val</span> j:<span class="type">Int</span> = <span class="number">20</span></span><br></pre></td></tr></table></figure>

<p><code>注意：能用常量的地方不用变量</code></p>
<h3 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala变量</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_TestVar</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//（1）声明变量时，类型可以省略，编译器自动推导，即类型推导</span></span><br><span class="line">    <span class="comment">//（2）类型确定后，就不能修改，说明Scala是强数据类型语言</span></span><br><span class="line">    <span class="comment">//（3）变量声明时，必须要有初始值</span></span><br><span class="line">    <span class="comment">//（4）在声明/定义一个变量时，可以使用var或者val来修饰，var修饰的变量可改变，val修改的变量不可改变</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> i: <span class="type">Int</span> = <span class="number">1</span></span><br><span class="line">    <span class="keyword">var</span> j = <span class="number">2</span></span><br><span class="line"><span class="comment">//    j = &quot;hello&quot; //报错，类型确定后，不可修改</span></span><br><span class="line"><span class="comment">//    var k: String //报错，必须设置初始值</span></span><br><span class="line">    <span class="keyword">val</span> l: <span class="type">String</span> = <span class="string">&quot;hello&quot;</span></span><br><span class="line"><span class="comment">//    l = &quot;world&quot; //报错，val修改变量不可改变</span></span><br><span class="line">          </span><br><span class="line">    <span class="comment">//补充</span></span><br><span class="line">    <span class="comment">//创建一个Student对象，并且使用val修饰</span></span><br><span class="line">    <span class="keyword">val</span> zhangsan: <span class="type">Student</span> = <span class="keyword">new</span> <span class="type">Student</span>(<span class="string">&quot;zhangsan&quot;</span>, <span class="number">12</span>)</span><br><span class="line"><span class="comment">//    zhangsan = new Student(&quot;lisi&quot;, 15) //报错，val修饰变量不可改变</span></span><br><span class="line">    <span class="comment">//val修饰对象，对象中的属性是可以修改的</span></span><br><span class="line">    zhangsan.age = <span class="number">15</span></span><br><span class="line">    zhangsan.printInfo()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、标识符的命名规范"><a href="#三、标识符的命名规范" class="headerlink" title="三、标识符的命名规范"></a>三、标识符的命名规范</h2><p>Scala对各种变量、方法、函数等命名时使用的字符序列称为标识符。即：凡是自己可以起名字的地方都叫标识符。</p>
<h3 id="命名规则"><a href="#命名规则" class="headerlink" title="命名规则"></a>命名规则</h3><ol>
<li><p>以字母或者下划线开头，后接字母、数字、下划线</p>
</li>
<li><p><strong>以操作符开头，且只包含操作符（+ - * &#x2F; # !等）</strong></p>
</li>
<li><p><strong>用反引号&#96;包括的任意字符串，即使是 Scala 关键字（39个）也可以</strong></p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span>, <span class="keyword">import</span>, <span class="class"><span class="keyword">class</span>, <span class="title">object</span>, <span class="title">trait</span>, <span class="keyword">extends</span>, <span class="keyword">with</span>, <span class="title">type</span>, <span class="title">for</span></span></span><br><span class="line"><span class="keyword">private</span>, <span class="keyword">protected</span>, <span class="keyword">abstract</span>, <span class="keyword">sealed</span>, <span class="keyword">final</span>, <span class="keyword">implicit</span>, <span class="keyword">lazy</span>, <span class="keyword">override</span></span><br><span class="line"><span class="keyword">try</span>, <span class="keyword">catch</span>, <span class="keyword">finally</span>, <span class="keyword">throw</span> </span><br><span class="line"><span class="keyword">if</span>, <span class="keyword">else</span>, <span class="keyword">match</span>, <span class="keyword">case</span>, <span class="keyword">do</span>, <span class="keyword">while</span>, <span class="keyword">for</span>, <span class="keyword">return</span>, <span class="keyword">yield</span></span><br><span class="line"><span class="function"><span class="keyword">def</span>, <span class="title">val</span>, <span class="title">var</span> </span></span><br><span class="line"><span class="keyword">this</span>, <span class="keyword">super</span></span><br><span class="line"><span class="keyword">new</span></span><br><span class="line"><span class="literal">true</span>, <span class="literal">false</span>, <span class="literal">null</span></span><br></pre></td></tr></table></figure>

<h3 id="示例-1"><a href="#示例-1" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala操作符和命名规范</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala03_Identifier</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// （1）以字母或者下划线开头，后接字母、数字、下划线</span></span><br><span class="line"><span class="comment">//        var hello: String = &quot;&quot; // ok</span></span><br><span class="line"><span class="comment">//        var Hello12: String = &quot;&quot; // ok</span></span><br><span class="line"><span class="comment">//        var 1hello: String = &quot;&quot; // error 数字不能开头</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        var h-b: String = &quot;&quot; // error   不能用-</span></span><br><span class="line"><span class="comment">//        var x h: String = &quot;&quot; // error   不能有空格</span></span><br><span class="line"><span class="comment">//        var h_4: String = &quot;&quot; // ok</span></span><br><span class="line"><span class="comment">//        var _ab: String = &quot;&quot; // ok</span></span><br><span class="line"><span class="comment">//        var Int: String = &quot;&quot; // ok 因为在Scala中Int是预定义的字符,不是关键字，但不推荐</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        var _: String = &quot;hello&quot; // ok 单独一个下划线不可以作为标识符，因为_被认为是一个方法</span></span><br><span class="line"><span class="comment">//        println(_)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//（2）以操作符开头，且只包含操作符（+ - * / # !等）</span></span><br><span class="line"><span class="comment">//        var +*-/#! : String = &quot;&quot; // ok</span></span><br><span class="line"><span class="comment">//        var +*-/#!1 : String = &quot;&quot; // error 以操作符开头，必须都是操作符</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//（3）用反引号`....`包括的任意字符串，即使是Scala关键字（39个）也可以</span></span><br><span class="line"><span class="comment">//        var if : String = &quot;&quot; // error 不能用关键字</span></span><br><span class="line"><span class="comment">//        var `if` : String = &quot;&quot; // ok 用反引号`....`包括的任意字符串,包括关键字</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、字符串输出"><a href="#四、字符串输出" class="headerlink" title="四、字符串输出"></a>四、字符串输出</h2><h3 id="基本语法-1"><a href="#基本语法-1" class="headerlink" title="基本语法"></a>基本语法</h3><ol>
<li>字符串，通过+号连接；</li>
<li>printf用法：字符串，通过%传值；</li>
<li>字符串模板：通过$获取变量值。</li>
</ol>
<h3 id="示例-2"><a href="#示例-2" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala字符串输出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala04_String</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> name: <span class="type">String</span> = <span class="string">&quot;zhangsan&quot;</span></span><br><span class="line">    <span class="keyword">var</span> age: <span class="type">Int</span> = <span class="number">18</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// （1）字符串，通过+号连接</span></span><br><span class="line">    println(name + <span class="string">&quot; &quot;</span> + age)</span><br><span class="line">    <span class="comment">// 补充：使用 * 重复拼接多次字符串</span></span><br><span class="line">    <span class="comment">// 表示将name的值重复拼接3次，并打印</span></span><br><span class="line">    println(name * <span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// （2）printf用法字符串，通过%传值。</span></span><br><span class="line">    printf(<span class="string">&quot;name=%s age=%d&quot;</span>, name, age)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// （3）字符串模板（插值字符串），通过$引用</span></span><br><span class="line">    <span class="keyword">var</span> s =</span><br><span class="line">      <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        |select name,age</span></span><br><span class="line"><span class="string">        |from user</span></span><br><span class="line"><span class="string">        |where name=&#x27;zhangsan&#x27;</span></span><br><span class="line"><span class="string">        |&quot;&quot;&quot;</span>.stripMargin</span><br><span class="line">    println(s)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> s1 =</span><br><span class="line">      <span class="string">s&quot;&quot;</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">         |select name,age</span></span><br><span class="line"><span class="string">         |from user</span></span><br><span class="line"><span class="string">         |where name=&quot;</span>$<span class="string">name&quot; and age&gt;<span class="subst">$&#123;age + 2&#125;</span></span></span><br><span class="line"><span class="string">         |&quot;</span><span class="string">&quot;&quot;</span>.stripMargin</span><br><span class="line">    println(s1)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 补充：</span></span><br><span class="line">    <span class="comment">// 一、前面添加f，%5.2f表示输出总长度为5，小数点后保留2位，且四舍五入</span></span><br><span class="line">    <span class="keyword">val</span> num: <span class="type">Double</span> = <span class="number">2.4256</span></span><br><span class="line">    println(</span><br><span class="line">      <span class="string">f&quot;&quot;</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">         |The number is $&#123;num&#125;%5.2f</span></span><br><span class="line"><span class="string">         |&quot;</span><span class="string">&quot;&quot;</span>.stripMargin)</span><br><span class="line">    <span class="comment">// 二、前面添加raw，表示无论写什么，都会原样输出</span></span><br><span class="line">    println(</span><br><span class="line">      <span class="string">raw&quot;&quot;</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">           |The number is $&#123;num&#125;%5.2f</span></span><br><span class="line"><span class="string">           |&quot;</span><span class="string">&quot;&quot;</span>.stripMargin)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、键盘输入"><a href="#五、键盘输入" class="headerlink" title="五、键盘输入"></a>五、键盘输入</h2><h3 id="基本语法-2"><a href="#基本语法-2" class="headerlink" title="基本语法"></a>基本语法</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">StdIn</span>.readLine()、<span class="type">StdIn</span>.readInt()、<span class="type">StdIn</span>.readDouble()</span><br></pre></td></tr></table></figure>

<h3 id="示例-3"><a href="#示例-3" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.io.<span class="type">StdIn</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala标准输入</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala05_StdIn</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;input your name:&quot;</span>)</span><br><span class="line">    <span class="keyword">val</span> name: <span class="type">String</span> = <span class="type">StdIn</span>.readLine()</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot;input your age:&quot;</span>)</span><br><span class="line">    <span class="keyword">val</span> age: <span class="type">Int</span> = <span class="type">StdIn</span>.readInt()</span><br><span class="line"></span><br><span class="line">    println(name + <span class="string">&quot; &quot;</span> + age)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="六、文件读写"><a href="#六、文件读写" class="headerlink" title="六、文件读写"></a>六、文件读写</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.&#123;<span class="type">File</span>, <span class="type">PrintWriter</span>&#125;</span><br><span class="line"><span class="keyword">import</span> scala.io.<span class="type">Source</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala读写文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala06_FileIO</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// 从文件中读取数据</span></span><br><span class="line">    <span class="type">Source</span>.fromFile(<span class="string">&quot;src/main/resources/test.txt&quot;</span>).foreach(print)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 将数据写入文件</span></span><br><span class="line">    <span class="keyword">val</span> writer: <span class="type">PrintWriter</span> = <span class="keyword">new</span> <span class="type">PrintWriter</span>(<span class="keyword">new</span> <span class="type">File</span>(<span class="string">&quot;src/main/resources/output.txt&quot;</span>))</span><br><span class="line">    writer.write(<span class="string">&quot;test write something to file&quot;</span>)</span><br><span class="line">    writer.close()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="数据类型（重点）"><a href="#数据类型（重点）" class="headerlink" title="数据类型（重点）"></a>数据类型（重点）</h1><h2 id="一、数据类型概述"><a href="#一、数据类型概述" class="headerlink" title="一、数据类型概述"></a>一、数据类型概述</h2><h3 id="Java-数据类型"><a href="#Java-数据类型" class="headerlink" title="Java 数据类型"></a>Java 数据类型</h3><p>Java 的基本数据类型：char、byte、short、int、long、float、double、boolean；</p>
<p>Java 的基本数据类型包装类：Character、Byte、Short、Integer、Long、Float、Double、Boolean；</p>
<p>Java 引用类型：对象类型；</p>
<p><strong>注：Java 中基本数据类型与引用类型没有共同的祖先；</strong></p>
<h3 id="Scala-数据类型"><a href="#Scala-数据类型" class="headerlink" title="Scala 数据类型"></a>Scala 数据类型</h3><ol>
<li>Scala 中一切数据都是对象，都是<strong>Any的子类</strong>。 </li>
<li>Scala 中数据类型分为两大类：<strong>数值类型（AnyVal）、引用类型（AnyRef）</strong>，<strong>不管是值类型还是引用类型都是对象</strong>。 </li>
<li>Scala 数据类型仍然遵守，<strong>低精度的值类型向高精度值类型，自动转换</strong>（隐式转换） 。</li>
<li>Scala 中的StringOps是对Java中的String增强 。</li>
<li>Unit：对应 Java 中的 void，用于方法返回值的位置，表示方法没有返回值。<strong>Unit 是一个数据类型</strong>，只有一个对象就是**()**。Void 不是数据类型，只是一个关键字 。</li>
<li><strong>Null 是一个类型</strong>，只有一个对象就是 <strong>null</strong>。<strong>它是所有引用类型（AnyRef）的子类</strong>。 </li>
<li><strong>Nothing，是所有数据类型的子类</strong>，主要用在一个函数没有明确返回值时使用，因为这样我们可以把抛出的返回值，返回给任何的变量或者函数。</li>
</ol>
<p><img src="/oct25-xxxxx/img/scala/04_Scala%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.png"></p>
<h2 id="二、整数类型（Byte-x2F-Short-x2F-Int-x2F-Long）"><a href="#二、整数类型（Byte-x2F-Short-x2F-Int-x2F-Long）" class="headerlink" title="二、整数类型（Byte&#x2F;Short&#x2F;Int&#x2F;Long）"></a>二、整数类型（Byte&#x2F;Short&#x2F;Int&#x2F;Long）</h2><p>Scala 的整数类型就是用于存放整数值的，比如12，30，3456等等。</p>
<h3 id="整数类型数值范围"><a href="#整数类型数值范围" class="headerlink" title="整数类型数值范围"></a>整数类型数值范围</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Byte [1]</td>
<td>8位有符号补码整数。数值区间为 -128 到 127</td>
</tr>
<tr>
<td>Short [2]</td>
<td>16位有符号补码整数。数值区间为 -32768 到 32767</td>
</tr>
<tr>
<td>Int [4]</td>
<td>32位有符号补码整数。数值区间为 -2147483648 到 2147483647</td>
</tr>
<tr>
<td>Long [8]</td>
<td>64位有符号补码整数。数值区间为 -9223372036854775808 到 9223372036854775807 &#x3D; 2的(64-1)次方-1</td>
</tr>
</tbody></table>
<h3 id="示例-4"><a href="#示例-4" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala整数类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala07_DataType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> n1: <span class="type">Byte</span> = <span class="number">127</span></span><br><span class="line">    <span class="keyword">var</span> n2: <span class="type">Byte</span> = <span class="number">-128</span></span><br><span class="line"><span class="comment">//    var n3: Byte = 128 // 超出 Byte 数值范围</span></span><br><span class="line"><span class="comment">//    var n4: Byte = -129</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> n5 = <span class="number">10</span> <span class="comment">// 整数型默认类型为 Int</span></span><br><span class="line">    <span class="keyword">var</span> n6 = <span class="number">123123</span>L <span class="comment">// Long 类型需要在数值后添加大/小写L</span></span><br><span class="line">          </span><br><span class="line">    <span class="keyword">val</span> b1: <span class="type">Byte</span> = <span class="number">10</span></span><br><span class="line">    <span class="comment">//IDEA编译器报错（10+20应该是Int类型），但是可以直接运行</span></span><br><span class="line">    <span class="comment">//因为Scala可以正常推断出10+20在Byte表达范围内</span></span><br><span class="line">    <span class="keyword">val</span> b2: <span class="type">Byte</span> = <span class="number">10</span> + <span class="number">20</span></span><br><span class="line">    println(b2)</span><br><span class="line"></span><br><span class="line"><span class="comment">//    val b3: Byte = b1 + 20 //报错，并无法运行，需要为Int</span></span><br><span class="line">    <span class="keyword">val</span> b3: <span class="type">Byte</span> = (b1 + <span class="number">20</span>).toByte</span><br><span class="line">    println(b3)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、浮点类型（Float-x2F-Double）"><a href="#三、浮点类型（Float-x2F-Double）" class="headerlink" title="三、浮点类型（Float&#x2F;Double）"></a>三、浮点类型（Float&#x2F;Double）</h2><p>Scala的浮点类型可以表示一个小数，比如123.4f，7.8，0.12等等。</p>
<h3 id="浮点数类型分类"><a href="#浮点数类型分类" class="headerlink" title="浮点数类型分类"></a>浮点数类型分类</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Float[4]</td>
<td>32 位,，IEEE 754标准的单精度浮点数</td>
</tr>
<tr>
<td>Double[8]</td>
<td>64 位，IEEE 754标准的双精度浮点数</td>
</tr>
</tbody></table>
<h3 id="示例-5"><a href="#示例-5" class="headerlink" title="示例"></a>示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala浮点类型 Float/Double</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala08_DataType02</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> n7: <span class="type">Float</span> = <span class="number">3.141592600</span>F</span><br><span class="line">    <span class="keyword">var</span> n8: <span class="type">Double</span> = <span class="number">3.141592600</span> <span class="comment">// 浮点型默认为 Double</span></span><br><span class="line"></span><br><span class="line">    println(n7) <span class="comment">// 输出：3.1415925</span></span><br><span class="line">    println(n8) <span class="comment">// 输出：3.1415926001</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、字符类型（Char）"><a href="#四、字符类型（Char）" class="headerlink" title="四、字符类型（Char）"></a>四、字符类型（Char）</h2><p>字符类型可以表示单个字符，字符类型是Char。</p>
<h3 id="示例-6"><a href="#示例-6" class="headerlink" title="示例"></a>示例</h3><p>（1）字符常量是用单引号 ‘ ‘ 括起来的单个字符。</p>
<p>（2）\t ：一个制表位，实现对齐的功能</p>
<p>（3）\n ：换行符</p>
<p>（4）\ ：表示\</p>
<p>（5）&quot; ：表示”</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala字符类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala09_CharType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (1) 字符常量是用单引号&#x27;&#x27; 括起来的单个字符</span></span><br><span class="line">    println(<span class="string">&quot;c1 = &quot;</span> + &#x27;c&#x27;)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (2) \t：一个制表符，实现对齐功能</span></span><br><span class="line">    println(<span class="string">&quot;zhangsan\t18 years old&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (3) \n：换行符</span></span><br><span class="line">    println(<span class="string">&quot;lisi\ngender is male&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (4) \\\\： 表示 \\</span></span><br><span class="line">    println(<span class="string">&quot;c:\\\\abc\\\\def&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (5) \\&quot;：表示 &quot;</span></span><br><span class="line">    println(<span class="string">&quot;wangwu say :\&quot;this is a apple\&quot;&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// （6）\\：表示\</span></span><br><span class="line">    println(<span class="string">&quot;wangwu\\say&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 补充</span></span><br><span class="line">    <span class="keyword">val</span> i1: <span class="type">Int</span> = &#x27;a&#x27;</span><br><span class="line">    println(i1)</span><br><span class="line">    <span class="keyword">val</span> i2: <span class="type">Int</span> = &#x27;<span class="number">0</span>&#x27;</span><br><span class="line">    println(i2)</span><br><span class="line">    <span class="keyword">val</span> i3: <span class="type">Char</span> = (i1 + <span class="number">1</span>).toChar</span><br><span class="line">    println(i3)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、布尔类型（Boolean）"><a href="#五、布尔类型（Boolean）" class="headerlink" title="五、布尔类型（Boolean）"></a>五、布尔类型（Boolean）</h2><p><strong>基本说明</strong></p>
<p>（1）布尔类型也叫Boolean类型，Booolean类型数据只允许取值true和false</p>
<p>（2）boolean类型占1个字节。</p>
<p><strong>示例</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala布尔类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala10_BooleanType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> isResult: <span class="type">Boolean</span> = <span class="literal">false</span></span><br><span class="line">    <span class="keyword">var</span> isSuccess: <span class="type">Boolean</span> = <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="六、Unit类型、Null类型和Nothing类型（重点）"><a href="#六、Unit类型、Null类型和Nothing类型（重点）" class="headerlink" title="六、Unit类型、Null类型和Nothing类型（重点）"></a>六、Unit类型、Null类型和Nothing类型（重点）</h2><h3 id="基本说明"><a href="#基本说明" class="headerlink" title="基本说明"></a>基本说明</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Unit</td>
<td>表示无值，和其他语言中void等同。用作不返回任何结果的方法的结果类型。Unit只有一个实例值，写成()。</td>
</tr>
<tr>
<td>Null</td>
<td>null , Null 类型只有一个实例值null</td>
</tr>
<tr>
<td>Nothing</td>
<td>Nothing类型在Scala的类层级最低端；它是任何其他类型的子类型。当一个函数，我们确定没有正常的返回值，可以用Nothing来指定返回类型，这样有一个好处，就是我们可以把返回的值（异常）赋给其它的函数或者变量（兼容性）</td>
</tr>
</tbody></table>
<h3 id="Unit"><a href="#Unit" class="headerlink" title="Unit"></a>Unit</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala 空值Unit</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * Unit只有一个实例，那就是 ()</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala11_UnitType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    println(sayOk()) <span class="comment">//输出：()</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">sayOk</span></span>(): <span class="type">Unit</span> = &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Null"><a href="#Null" class="headerlink" title="Null"></a>Null</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala 空引用Null</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala12_NullType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// null 可以赋值给任意引用类型（AnyRef）</span></span><br><span class="line">    <span class="comment">// 但是不能赋值给值类型（AnyVal）</span></span><br><span class="line">    <span class="keyword">var</span> cat = <span class="keyword">new</span> <span class="type">Cat</span>()</span><br><span class="line">    cat = <span class="literal">null</span> <span class="comment">// ok</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//    var n1: Int = null // error，编译报错，原因：null为引用类型的子类</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Nothing"><a href="#Nothing" class="headerlink" title="Nothing"></a>Nothing</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala Nothing</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala13_NothingType</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">val</span> i: <span class="type">Int</span> = test2(<span class="number">2</span>)</span><br><span class="line">    println(i)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 抛异常时，可以使用Nothing作为返回值</span></span><br><span class="line"><span class="comment">   * 但是直接这样写是没有意义的</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">test1</span></span>(): <span class="type">Nothing</span> = &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="type">Exception</span>()</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * if分支返回值为 Nothing，而else分支则是Int</span></span><br><span class="line"><span class="comment">   * 由于Nothing是所有类型的子类，所以方法可以返回Int</span></span><br><span class="line"><span class="comment">   * 这样抛异常时，使用Int作为返回值，语法没问题，因为Nothing也是Int的子类</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param n</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">test2</span></span>(n: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">0</span>)</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> <span class="type">Exception</span>()</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">      <span class="keyword">return</span> n</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="七、类型转换"><a href="#七、类型转换" class="headerlink" title="七、类型转换"></a>七、类型转换</h2><h3 id="数值类型自动转换"><a href="#数值类型自动转换" class="headerlink" title="数值类型自动转换"></a>数值类型自动转换</h3><p>当 Scala 程序在进行赋值或者运算时，<strong>精度小的</strong>类型<strong>自动转换为精度大的数值</strong>类型，这个就是自动类型转换（隐式转换）。数据类型按精度（容量）大小排序为：</p>
<p><img src="/oct25-xxxxx/img/scala/05_%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%87%AA%E5%8A%A8%E8%BD%AC%E6%8D%A2.png"></p>
<h3 id="基本说明-1"><a href="#基本说明-1" class="headerlink" title="基本说明"></a>基本说明</h3><ol>
<li><p>自动提升原则：有多种类型的数据混合运算时，系统首先<strong>自动将所有数据转换成精度大的那种数据类型</strong>，然后再进行计算。</p>
</li>
<li><p><strong>把精度大的数值类型赋值给精度小的数值类型时，就会报错</strong>，反之就会进行自动类型转换。</p>
</li>
<li><p><strong>Byte 和 Short 数据类型与 Char 之间不会相互自动转换</strong>。</p>
</li>
<li><p><strong>Byte，Short，Char</strong> 他们三者可以计算，<strong>在计算时首先转换为 Int 类型</strong>。</p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala类型转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala14_DataTypeConversion</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (1) 自动类型提升的原则：有多种类型的数据混合运算时，系统首先自动将所有的数据类型转换成精度最大的数据类型，然后再进行计算</span></span><br><span class="line">    <span class="keyword">var</span> n1 = <span class="number">1</span> + <span class="number">2.0</span></span><br><span class="line">    println(n1) <span class="comment">// n1 为 Double 类型</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (2) 把精度大的数值类型赋值给精度小的数值类型时，就会报错，反之就会进行自动类型转换</span></span><br><span class="line">    <span class="keyword">var</span> n2: <span class="type">Double</span> = <span class="number">1.0</span></span><br><span class="line"><span class="comment">//    var n3 : Int = n2 // 错误，Double 的精度更高</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (3) Byte 和 Short 类型与 Char 类型之间不能相互自动类型转换</span></span><br><span class="line">    <span class="keyword">var</span> n4: <span class="type">Byte</span> = <span class="number">1</span></span><br><span class="line"><span class="comment">//    var c1: Char = n4 // 错误</span></span><br><span class="line">    <span class="keyword">var</span> n5: <span class="type">Int</span> = n4</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (4) Byte, Short 和 Char 他们三者可以计算，在计算时首先转换为 Int 类型</span></span><br><span class="line">    <span class="keyword">var</span> n6: <span class="type">Byte</span> = <span class="number">1</span></span><br><span class="line">    <span class="keyword">var</span> c2: <span class="type">Char</span> = <span class="number">1</span></span><br><span class="line"><span class="comment">//    var n7: Short = n6 + c2 // 错误，n6+c2结果为 Int，赋值给 Short 类型，精度大的赋值给精确度小的会报错</span></span><br><span class="line">    <span class="keyword">var</span> n8: <span class="type">Int</span> = n6 + c2</span><br><span class="line"><span class="comment">//    var n9: Short = 10 + 90 // 数值默认类型为 Int，赋值给 Short 类型报错</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h3><h4 id="ⅰ基本说明"><a href="#ⅰ基本说明" class="headerlink" title="ⅰ基本说明"></a>ⅰ基本说明</h4><p>自动类型转换的逆过程，将精度大的数值类型转换为精度小的数值类型。使用时要加上强制转函数，但可能造成精度降低或溢出，格外要注意。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Java</span>：int num = (int)<span class="number">2.5</span>;</span><br><span class="line"><span class="type">Scala</span>：<span class="keyword">var</span> num : <span class="type">Int</span> = <span class="number">2.7</span>.toInt</span><br></pre></td></tr></table></figure>

<h4 id="ⅱ示例"><a href="#ⅱ示例" class="headerlink" title="ⅱ示例"></a>ⅱ示例</h4><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala强制类型转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala15_DataTypeForceConversion</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (1) 将数据由高精度转换为低精度，就需要使用到强制转换</span></span><br><span class="line">    <span class="comment">// 注：会损失精度</span></span><br><span class="line">    <span class="keyword">var</span> n1: <span class="type">Int</span> = <span class="number">2.5</span>.toInt</span><br><span class="line">    println(n1) <span class="comment">// 2</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (2) 强转符号只针对于最近的操作数有效，往往会使用小括号提升优先级</span></span><br><span class="line">    <span class="keyword">var</span> r1: <span class="type">Int</span> = <span class="number">10</span> * <span class="number">3.5</span>.toInt + <span class="number">6</span> * <span class="number">1.5</span>.toInt</span><br><span class="line">    println(r1) <span class="comment">// 10 * 3 + 6 * 1 = 36</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> r2: <span class="type">Int</span> = (<span class="number">10</span> * <span class="number">3.5</span> + <span class="number">6</span> * <span class="number">1.5</span>).toInt</span><br><span class="line">    println(r2) <span class="comment">// 44.0.toInt = 44</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="数值类型和-String-类型间的转换"><a href="#数值类型和-String-类型间的转换" class="headerlink" title="数值类型和 String 类型间的转换"></a>数值类型和 String 类型间的转换</h3><h4 id="ⅰ基本说明-1"><a href="#ⅰ基本说明-1" class="headerlink" title="ⅰ基本说明"></a>ⅰ基本说明</h4><p>在程序开发中，我们经常需要将基本数值类型转成 String 类型。或者将 String 类型转成基本数值类型。</p>
<p><strong>示例</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala数值类型和String类型间转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala16_StringConversion</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (1) 基本类型转String类型（语法：将基本类型的值+&quot;&quot; 即可）</span></span><br><span class="line">    <span class="keyword">val</span> str1: <span class="type">String</span> = <span class="literal">true</span> + <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="keyword">val</span> str2: <span class="type">String</span> = <span class="number">4.5</span> + <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="keyword">val</span> str3: <span class="type">String</span> = <span class="number">100</span> + <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="keyword">val</span> str4: <span class="type">String</span> = <span class="number">12.</span>toString</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  (2) String类型转基本数值类型（语法：调用相关API）</span></span><br><span class="line">    <span class="comment">// 注：&quot;12a&quot;.toInt 会报错</span></span><br><span class="line">    <span class="keyword">var</span> s1: <span class="type">String</span> = <span class="string">&quot;12&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> n1: <span class="type">Byte</span> = s1.toByte</span><br><span class="line">    <span class="keyword">val</span> n2: <span class="type">Int</span> = s1.toInt</span><br><span class="line">    <span class="keyword">val</span> n3: <span class="type">Long</span> = s1.toLong</span><br><span class="line">    <span class="keyword">val</span> n4: <span class="type">Int</span> = <span class="string">&quot;12.3&quot;</span>.toDouble.toInt</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="ⅱ注意事项"><a href="#ⅱ注意事项" class="headerlink" title="ⅱ注意事项"></a>ⅱ注意事项</h3><p>在将String类型转成基本数值类型时，要<strong>确保 String 类型能够转成有效的数据</strong>，比如我们可以把”123”，转成一个整数，但是不能把”hello”转成一个整数。</p>
<p>var n5:Int &#x3D; “12.6”.toInt会出现NumberFormatException异常。</p>
<h2 id="八、强制类型转换案例"><a href="#八、强制类型转换案例" class="headerlink" title="八、强制类型转换案例"></a>八、强制类型转换案例</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter02</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO 案例</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala17_Sample</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> n: <span class="type">Int</span> = <span class="number">130</span></span><br><span class="line">    <span class="keyword">val</span> b: <span class="type">Byte</span> = n.toByte</span><br><span class="line">    println(b) <span class="comment">//打印结果：-126</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 128: Int类型，占4个字节，就是32位</span></span><br><span class="line"><span class="comment">     * 128原码：0000 0000 0000 0000 0000 0000 1000 0000</span></span><br><span class="line"><span class="comment">     * 128补码：0000 0000 0000 0000 0000 0000 1000 0000</span></span><br><span class="line"><span class="comment">     * 截取最后一个字节Byte</span></span><br><span class="line"><span class="comment">     * 得到补码：1000 0000 ==》 -128</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 130: Int类型，占4个字节，就是32位</span></span><br><span class="line"><span class="comment">     * 130原码：0000 0000 0000 0000 0000 0000 1000 0010</span></span><br><span class="line"><span class="comment">     * 130补码：0000 0000 0000 0000 0000 0000 1000 0010</span></span><br><span class="line"><span class="comment">     * 截取最后一个字节Byte</span></span><br><span class="line"><span class="comment">     * 得到补码：1000 0010</span></span><br><span class="line"><span class="comment">     * 转为原码：1111 1110 ==》 -126</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>











</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/">https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Scala/">Scala</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt45.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据之多范式编程语言 Scala03_运算符</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2024/02/18/44%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala01_%E5%85%A5%E9%97%A8/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt44.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据之多范式编程语言 Scala01_入门</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2024/02/18/44%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala01_%E5%85%A5%E9%97%A8/" title="大数据之多范式编程语言 Scala01_入门"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt44.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala01_入门</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/" title="大数据之多范式编程语言 Scala03_运算符"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-19</div><div class="title">大数据之多范式编程语言 Scala03_运算符</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/" title="大数据之多范式编程语言 Scala04_流程控制"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt47.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala04_流程控制</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt48.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala05_函数式编程</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-21</div><div class="title">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt50.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-22</div><div class="title">大数据之多范式编程语言 Scala07_集合</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Scala-%E7%9A%84%E5%8F%98%E9%87%8F"><span class="toc-number">1.</span> <span class="toc-text">Scala 的变量</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%B3%A8%E9%87%8A"><span class="toc-number">1.1.</span> <span class="toc-text">一、注释</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%98%E9%87%8F%E5%92%8C%E5%B8%B8%E9%87%8F"><span class="toc-number">1.2.</span> <span class="toc-text">二、变量和常量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">1.2.1.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.2.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%A0%87%E8%AF%86%E7%AC%A6%E7%9A%84%E5%91%BD%E5%90%8D%E8%A7%84%E8%8C%83"><span class="toc-number">1.3.</span> <span class="toc-text">三、标识符的命名规范</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="toc-number">1.3.1.</span> <span class="toc-text">命名规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-1"><span class="toc-number">1.3.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BE%93%E5%87%BA"><span class="toc-number">1.4.</span> <span class="toc-text">四、字符串输出</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95-1"><span class="toc-number">1.4.1.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-2"><span class="toc-number">1.4.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E9%94%AE%E7%9B%98%E8%BE%93%E5%85%A5"><span class="toc-number">1.5.</span> <span class="toc-text">五、键盘输入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95-2"><span class="toc-number">1.5.1.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-3"><span class="toc-number">1.5.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E6%96%87%E4%BB%B6%E8%AF%BB%E5%86%99"><span class="toc-number">1.6.</span> <span class="toc-text">六、文件读写</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%89"><span class="toc-number">2.</span> <span class="toc-text">数据类型（重点）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%A6%82%E8%BF%B0"><span class="toc-number">2.1.</span> <span class="toc-text">一、数据类型概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Java-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.1.1.</span> <span class="toc-text">Java 数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Scala-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.1.2.</span> <span class="toc-text">Scala 数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%95%B4%E6%95%B0%E7%B1%BB%E5%9E%8B%EF%BC%88Byte-x2F-Short-x2F-Int-x2F-Long%EF%BC%89"><span class="toc-number">2.2.</span> <span class="toc-text">二、整数类型（Byte&#x2F;Short&#x2F;Int&#x2F;Long）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B4%E6%95%B0%E7%B1%BB%E5%9E%8B%E6%95%B0%E5%80%BC%E8%8C%83%E5%9B%B4"><span class="toc-number">2.2.1.</span> <span class="toc-text">整数类型数值范围</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-4"><span class="toc-number">2.2.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%B5%AE%E7%82%B9%E7%B1%BB%E5%9E%8B%EF%BC%88Float-x2F-Double%EF%BC%89"><span class="toc-number">2.3.</span> <span class="toc-text">三、浮点类型（Float&#x2F;Double）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E7%B1%BB%E5%9E%8B%E5%88%86%E7%B1%BB"><span class="toc-number">2.3.1.</span> <span class="toc-text">浮点数类型分类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-5"><span class="toc-number">2.3.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%AD%97%E7%AC%A6%E7%B1%BB%E5%9E%8B%EF%BC%88Char%EF%BC%89"><span class="toc-number">2.4.</span> <span class="toc-text">四、字符类型（Char）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-6"><span class="toc-number">2.4.1.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%B8%83%E5%B0%94%E7%B1%BB%E5%9E%8B%EF%BC%88Boolean%EF%BC%89"><span class="toc-number">2.5.</span> <span class="toc-text">五、布尔类型（Boolean）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81Unit%E7%B1%BB%E5%9E%8B%E3%80%81Null%E7%B1%BB%E5%9E%8B%E5%92%8CNothing%E7%B1%BB%E5%9E%8B%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%89"><span class="toc-number">2.6.</span> <span class="toc-text">六、Unit类型、Null类型和Nothing类型（重点）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E"><span class="toc-number">2.6.1.</span> <span class="toc-text">基本说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Unit"><span class="toc-number">2.6.2.</span> <span class="toc-text">Unit</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Null"><span class="toc-number">2.6.3.</span> <span class="toc-text">Null</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Nothing"><span class="toc-number">2.6.4.</span> <span class="toc-text">Nothing</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.7.</span> <span class="toc-text">七、类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E8%87%AA%E5%8A%A8%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.7.1.</span> <span class="toc-text">数值类型自动转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E-1"><span class="toc-number">2.7.2.</span> <span class="toc-text">基本说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.7.3.</span> <span class="toc-text">强制类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E2%85%B0%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E"><span class="toc-number">2.7.3.1.</span> <span class="toc-text">ⅰ基本说明</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E2%85%B1%E7%A4%BA%E4%BE%8B"><span class="toc-number">2.7.3.2.</span> <span class="toc-text">ⅱ示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%92%8C-String-%E7%B1%BB%E5%9E%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.7.4.</span> <span class="toc-text">数值类型和 String 类型间的转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E2%85%B0%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E-1"><span class="toc-number">2.7.4.1.</span> <span class="toc-text">ⅰ基本说明</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E2%85%B1%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">2.7.5.</span> <span class="toc-text">ⅱ注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E6%A1%88%E4%BE%8B"><span class="toc-number">2.8.</span> <span class="toc-text">八、强制类型转换案例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg19.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>